package cn.school.demo.ui.collect

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Article
import cn.school.demo.pojo.Collect
import cn.school.demo.pojo.Result
import cn.school.demo.repository.CollectRepository
import com.blankj.utilcode.util.LogUtils

class CollectViewModel : ViewModel() {

    private val _collectList = mutableListOf<Collect>()
    val collectList: MutableList<Collect> get() = _collectList

    private val _isOk = MutableLiveData<Boolean>()
    val isOk: LiveData<Boolean> get() = _isOk

    private val _isEdit = MutableLiveData<Boolean>(false)
    val isEdit: LiveData<Boolean> get() = _isEdit

    private val _deleteList = mutableListOf<Collect>()
    val deleteList: MutableList<Collect> get() = _deleteList

    fun getCollectListById(): LiveData<MutableList<Collect>> =
        CollectRepository().getCollectList(MyApplication.user!!.sfNumber)

    fun setCollectList(list: MutableList<Collect>) {
        if (list.size > 0) {
            LogUtils.e(list)
            _collectList.clear()
            _collectList.addAll(list)
        } else {
            _collectList.clear()
        }
        _isOk.value = if (_isOk.value == null) false else !_isOk.value!!
    }

    fun addDeleteList(collect: Collect, what: Boolean) {
        if (what)
            _deleteList.add(collect)
        else
            _deleteList.remove(collect)
    }

    fun updateEdit() {
        _isEdit.value = !_isEdit.value!!
    }

    fun delete(): LiveData<Result> =
        CollectRepository().deleteCollectList(_deleteList)

    fun getArticle(id: Int): LiveData<Article> = CollectRepository().getArticleById(id)
}